package com.xuxueli.applyModules.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xuxueli.applyModules.entity.TestCommontData;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * List<object>转 List<Map<String, Object>>
 */
public class ListObjectToMap {

    public static <T> List<Map<String, Object>> convertData(List<Map<String, Object>> mapList, List<T> list){
        for (T object:list){
            // 将 实体类 转换为 Map
            Map<String,Object> map = JSON.parseObject(JSON.toJSONString(object, SerializerFeature.WriteMapNullValue), Map.class);
            mapList.add(map);
        }
        return mapList;
    }

    public static <T> List<Map<String, Object>> convertDataDate(List<Map<String, Object>> mapList, List<T> list){
        for (T object:list){
            // 将 实体类 转换为 Map
            Map<String,Object> map = JSON.parseObject(JSON.toJSONString(object, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat), Map.class);
            mapList.add(map);
        }
        return mapList;
    }

    public static void main(String[] args) {
        List<TestCommontData> list = new ArrayList<>();
        TestCommontData testData = new TestCommontData();
        testData.setName("测试");
        testData.setTime(new Date());
        TestCommontData testDatas = new TestCommontData();
        testDatas.setName("测试数据");
        testDatas.setAmount(BigDecimal.ONE);
        testDatas.setTimeNow(new Date());
        list.add(testData);
        list.add(testDatas);
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<Map<String, Object>> mapDateList = new ArrayList<>();
        List<Map<String, Object>> finalList= convertData(mapList, list);
        List<Map<String, Object>> finalListDate = convertDataDate(mapDateList, list);
        System.out.println(finalList.size()+":"+finalListDate.size());
    }

}
